package com.mcarrot.site.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.mcarrot.site.common.CommonUtils;
import com.mcarrot.site.dao.AffairDAO;
import com.mcarrot.site.model.Affair;

public class AffairServiceImpl implements com.mcarrot.site.service.AffairService {
	
	private AffairDAO affairDAO;

	@Override
	public Long saveOrUpdate(Affair affair) {
		Long code = affairDAO.saveOrUpdate(affair);
		return code;
	}

	@Override
	public Long del(Integer affairId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Affair findById(Integer affairId) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public Integer getCount(String key, Date startDate, Date endDate) {
		Integer count = 0;
		if(CommonUtils.isNotEmptyStr(key) && CommonUtils.isNotEmptyObj(startDate) 
				&& CommonUtils.isNotEmptyObj(endDate)){
			String start = CommonUtils.formatDate(startDate, "yyyy-MM-dd");
			String end = CommonUtils.formatDate(endDate, "yyyy-MM-dd");
			count = affairDAO.getCountByConditions(key, start, end);
		}else if(CommonUtils.isNotEmptyStr(key) && CommonUtils.isNotEmptyObj(startDate)){
			String start = CommonUtils.formatDate(startDate, "yyyy-MM-dd");
			count = affairDAO.getCountByKeyAndStartDate(key, start);
		}else if(CommonUtils.isNotEmptyObj(key) && CommonUtils.isNotEmptyObj(endDate)){
			String end = CommonUtils.formatDate(endDate, "yyyy-MM-dd");
			count = affairDAO.getCountByKeyAndEndDate(key, end);
		}else if(CommonUtils.isNotEmptyStr(key)){
			count = affairDAO.getCountByKey(key);
		}else if(CommonUtils.isNotEmptyObj(startDate)){
			String start = CommonUtils.formatDate(startDate, "yyyy-MM-dd");
			count = affairDAO.getCountByStartDate(start);
		}else if(CommonUtils.isNotEmptyObj(endDate)){
			String end = CommonUtils.formatDate(endDate, "yyyy-MM-dd");
			count = affairDAO.getCountByStartDate(end);
		}else{
			count = affairDAO.getCount();
		}
		return count;
	}

	@Override
	public List<Affair> listAffairs(String key, Date startDate, Date endDate, 
			Integer startIndex, Integer limitIndex) {
		List<Affair> affairs = new ArrayList<Affair>();
		if(CommonUtils.isNotEmptyStr(key) && CommonUtils.isNotEmptyObj(startDate)
				&& CommonUtils.isNotEmptyObj(endDate)){
			String start = CommonUtils.formatDate(startDate, "yyyy-MM-dd");
			String end = CommonUtils.formatDate(endDate, "yyyy-MM-dd");
			affairs = affairDAO.listByConditions(key, start, end, startIndex, limitIndex);
		}else if(CommonUtils.isNotEmptyStr(key) && CommonUtils.isNotEmptyObj(startDate)){
			String start = CommonUtils.formatDate(startDate, "yyyy-MM-dd");
			affairs = affairDAO.listByKeyAndStartDate(key, start, startIndex, limitIndex);
		}else if(CommonUtils.isNotEmptyObj(key) && CommonUtils.isNotEmptyObj(endDate)){
			String end = CommonUtils.formatDate(endDate, "yyyy-MM-dd");
			affairs = affairDAO.listByKeyAndEndDate(key, end, startIndex, limitIndex);
		}else if(CommonUtils.isNotEmptyStr(key)){
			affairs = affairDAO.listByKey(key, startIndex, limitIndex);
		}else if(CommonUtils.isNotEmptyObj(startDate)){
			String start = CommonUtils.formatDate(startDate, "yyyy-MM-dd");
			affairs = affairDAO.listByStartDate(start, startIndex, limitIndex);
		}else if(CommonUtils.isNotEmptyObj(endDate)){
			String end = CommonUtils.formatDate(endDate, "yyyy-MM-dd");
			affairs = affairDAO.listByEndDate(end, startIndex, limitIndex);
		}else{
			affairs = affairDAO.list(startIndex, limitIndex);
		}
		return affairs;
	}
	

	public AffairDAO getAffairDAO() {
		return affairDAO;
	}

	public void setAffairDAO(AffairDAO affairDAO) {
		this.affairDAO = affairDAO;
	}

}
